<?php

namespace App\Http\Controllers\Api;


use App\Models\Chapter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use App\Validate\api\ChapterValidate;
use App\Common\ConstCode;
/**
 * Class ChapterController 漫画项目章节控制器
 * @package App\Http\Controllers\Api
 */
class ChapterController extends BaseController
{
    public $successStatus=200;
    protected $chapter;
    protected $v;
    /**
     * api注册
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */

    public function __construct(Chapter $chapter,ChapterValidate $v)
    {
        $this->chapter = $chapter;
        $this->v = $v;
    }



    public function create(Request $request){
        $this->v->validate('chapter');
        $data=$request->input();
        $bool=$this->chapter->create($data);
        if($bool){
            return response()->json(['code'=>1,'msg'=>'添加成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'添加失败']);
        }
        
    }

    public function sort(Request $request){
        $this->v->validate('sort');
        $data=$request->input();
        $bool=$this->chapter->sort($data);
        if($bool){
            return response()->json(['code'=>1,'msg'=>'替换成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'替换失败']);
        }
    }

    public function list(Request $request){
        $this->v->validate('list');
        $data=$request->input();
        $data['page'] = $request->input('page',ConstCode::PAGE);
        $data['pageSize']  = $request->input('pageSize',ConstCode::PER_PAGE);
        $result=$this->chapter->list($data);
        if($result){
            return response()->json(['code'=>1,'data'=>$result]);
        }else{
            return response()->json(['code'=>0,'msg'=>'查询失败']);
        }
    }

    public function del(Request $request){
        $this->v->validate('del');
        $data=$request->input();
        $bool=$this->chapter->del($data['id']);
        if($bool){
            return response()->json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'删除失败']);
        }
    }

    public function savePic(Request $request){
        $this->v->validate('savePic');
        $data=$request->input();
        $data['location_id'] = $request->input('location_id',0);
        $data['second_pic_uri'] = $request->input('second_pic_uri','');
        $data['second_matter_list'] = $request->input('second_matter_list','');
        $bool=$this->chapter->savePic($data);
        if($bool){
            return response()->json(['code'=>1,'msg'=>'保存成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'保存失败']);
        }
    }

    public function picInfo(Request $request){
        $this->v->validate('picInfo');
        $data=$request->input();
        $res=$this->chapter->picInfo($data);
        if($res){
            return response()->json(['code'=>1,'data'=>$res]);
        }else{
            return response()->json(['code'=>0,'msg'=>'数据不存在']);
        }
    }


    public function picEdit(Request $request){
        $this->v->validate('picEdit');
        $data=$request->input();
        $bool=$this->chapter->picEdit($data);
        if($bool){
            return response()->json(['code'=>1,'msg'=>'保存成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'保存失败']);
        }
    }

    public function picList(Request $request){
        $this->v->validate('list');
        $data=$request->input();
        $data['page'] = $request->input('page',ConstCode::PAGE);
        $data['pageSize']  = $request->input('pageSize',ConstCode::PER_PAGE);
        $result=$this->chapter->picList($data);
        if($result){
            return response()->json(['code'=>1,'data'=>$result]);
        }else{
            return response()->json(['code'=>0,'msg'=>'查询失败']);
        }
    }

    public function picDel(Request $request){
        $this->v->validate('del');
        $data=$request->input();
        $bool=$this->chapter->picDel($data['id']);
        if($bool){
            return response()->json(['code'=>1,'msg'=>'删除成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'删除失败']);
        }
    }

    public function movePic(Request $request){
//        $this->v->validate('move');
//        $data=$request->input();
        //todo 图片移动
    }
}
